//
//  SelectDeliveryMethyVc.m
//  BMProject
//
//  Created by 段大志 on 16/1/2.
//  Copyright (c) 2016 BMProject. All rights reserved.
//

#import "SelectDeliveryMethyVc.h"
#import "UIViewController+common.h"
#import "DataModel.h"
#import "OrderVc.h"

@interface SelectDeliveryMethyVc ()<UITextViewDelegate>{
    NSString * _sendType;
}
@property (weak, nonatomic) IBOutlet UITextField *nameTf;
@property (weak, nonatomic) IBOutlet UITextField *numTf;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *remarkLabelMarginTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *remarkMarginTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelOneConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelTwoConstaint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewOneHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTwoHeight;
@property (weak, nonatomic) IBOutlet UIButton *expressBtn;
@property (weak, nonatomic) IBOutlet UIButton *sendBySelfBtn;
@property (weak, nonatomic) IBOutlet UITextView *remarkTv;
@property (weak, nonatomic) IBOutlet UILabel *limitTv;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;
@end

@implementation SelectDeliveryMethyVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars =NO;
    self.modalPresentationCapturesStatusBarAppearance =NO;
    self.navigationController.navigationBar.translucent =NO;

    NAV_INIT(self, @"配送信息", @"back", @selector(backButtonClick:),nil, nil);
    // Do any additional setup after loading the view from its nib.
    self.sendBtn.layer.cornerRadius = 5;
    self.remarkTv.delegate = self;
    [self chooseSendBySelf:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)sendAction:(id)sender {
    if([@"1" isEqualToString:_sendType]){
        [UserServices orderAuditOptByToken:[[AppDelegate appDelegate] getUserInfo].token
                                   basicId:_model.id
                                       opt:@"sub"
                               expressName:@""
                                expressNum:@""
                             expressRemark:_remarkTv.text
                                  sendType:_sendType
                                showDialog:YES
                           completionBlock:^(int result, id responseObject) {
                               for (int i = 0 ;i < self.navigationController.viewControllers.count; ++i) {
                                   UIViewController *vc = self.navigationController.viewControllers[i];
                                   if ([vc isKindOfClass:[OrderVc class]]) {
                                       [self.navigationController popToViewController:vc animated:YES];
                                   }
                               }

                           }];

    }else{
        if(IsStringEmpty(self.nameTf.text)){
            [SVProgressHUD showErrorWithStatus:@"快递名称不能为空"];
            return;
        }
        if(IsStringEmpty(self.numTf.text)){
            [SVProgressHUD showErrorWithStatus:@"快递单号不能为空"];
            return;
        }
        [UserServices orderAuditOptByToken:[[AppDelegate appDelegate] getUserInfo].token
                                   basicId:_model.id
                                       opt:@"sub"
                               expressName:_nameTf.text
                                expressNum:_numTf.text
                             expressRemark:_remarkTv.text
                                  sendType:_sendType
                                showDialog:YES
                           completionBlock:^(int result, id responseObject) {
                               for (int i = 0 ;i < self.navigationController.viewControllers.count; ++i) {
                                   UIViewController *vc = self.navigationController.viewControllers[i];
                                   if ([vc isKindOfClass:[OrderVc class]]) {
                                       [self.navigationController popToViewController:vc animated:YES];
                                   }
                               }

                           }];
    }

}
- (IBAction)selfAction:(id)sender {
    [self.sendBySelfBtn setImage:[UIImage imageNamed:@"circle_sel"] forState:UIControlStateNormal];
    [self.expressBtn setImage:[UIImage imageNamed:@"promiseCircle"] forState:UIControlStateNormal];
    [self chooseSendBySelf:YES];
}
- (IBAction)expressAction:(id)sender {
    [self.sendBySelfBtn setImage:[UIImage imageNamed:@"promiseCircle"] forState:UIControlStateNormal];
    [self.expressBtn setImage:[UIImage imageNamed:@"circle_sel"] forState:UIControlStateNormal];
    [self chooseSendBySelf:NO];
}

- (void)chooseSendBySelf:(BOOL)isSelf{
    if(isSelf){
        _labelOneConstraint.constant = 0;
        _labelTwoConstaint.constant = 0;
        _viewTwoHeight.constant = 0;
        _viewOneHeight.constant = 0;
        _topViewHeightConstraint.constant = 170;
        _remarkLabelMarginTopConstraint.constant = 10;
        _remarkMarginTopConstraint.constant = 10;
        _sendType = @"1";
    }else{
        _labelOneConstraint.constant = 17;
        _labelTwoConstaint.constant = 17;
        _viewOneHeight.constant = 25;
        _viewTwoHeight.constant = 25;
        _topViewHeightConstraint.constant = 230;
        _remarkLabelMarginTopConstraint.constant = 79;
        _remarkMarginTopConstraint.constant = 74;
        _sendType = @"2";
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    int num = 50 - textView.text.length;
    self.limitTv.text = [[@"您还可输入" stringByAppendingString:kIntToString(num)] stringByAppendingString:@"个字符"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
